package com.appspot.mypartymanager.client;

import com.appspot.mypartymanager.entity.party.Party;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class MyPartyManager implements EntryPoint {

	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Action service.
	 */
	private final ActionServiceAsync actionService = GWT
			.create(ActionService.class);

	// aplication panels
	private DockPanel mainPanel = new DockPanel();
	private DecoratorPanel messagePanel = new DecoratorPanel();

	// login widgets
	private LoginInfo loginInfo = null;

	private MenuBar topMenu = new MenuBar(false);

	// menu commands
	Command menuCommandDoNothing = new Command() {
		public void execute() {
		}
	};

	Command menuCommandSignIn = new Command() {
		public void execute() {
			Window.Location.replace(loginInfo.getLoginUrl());
		}
	};

	Command menuCommandSignOut = new Command() {
		public void execute() {
			Window.Location.replace(loginInfo.getLogoutUrl());
		}
	};

	Command menuCommandNewParty = new Command() {
		public void execute() {
			/**
			 * TODO implement
			 */
		}
	};

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		// assemble messagePanel
		// Button messagePanelHideButton = new Button("hide");
		// messagePanel.setVisible(false);

		// assemble the main layout
		mainPanel.add(messagePanel, DockPanel.NORTH);
		mainPanel.setCellHorizontalAlignment(topMenu, DockPanel.ALIGN_CENTER);

		mainPanel.add(topMenu, DockPanel.NORTH);
		mainPanel.setWidth("100%");
		mainPanel.setCellHorizontalAlignment(topMenu, DockPanel.ALIGN_RIGHT);
		RootPanel.get().add(mainPanel);

		// Check login status using login service.
		LoginServiceAsync loginService = GWT.create(LoginService.class);
		loginService.login(GWT.getHostPageBaseURL(),
				new AsyncCallback<LoginInfo>() {
					public void onFailure(Throwable error) {
					}

					public void onSuccess(LoginInfo result) {
						loginInfo = result;
						if (loginInfo.isLoggedIn()) {
							loadMyPartyManager();
						} else {
							loadLogin();
						}
					}
				});
	}

	protected void loadMyPartyManager() {

		// set up top menu
		topMenu.addItem(loginInfo.getEmailAddress(), true, menuCommandDoNothing);
		topMenu.addSeparator();

		// load user party's
		actionService.getPartysByUser(loginInfo.getEmailAddress(),
				new AsyncCallback<Party>() {
					public void onFailure(Throwable caught) {

						Window.alert("SERVER_ERROR");
					}

					public void onSuccess(Party result) {
						Party p = result;
						Window.alert(p.getName());
					}
				});

		/*
		// check if user has partys
		if (userPartys.size() > 0) {
			MenuBar partysMenu = new MenuBar(true);
			partysMenu.setTitle("My Partys");

			for (String key : userPartys.keySet()) {

				partysMenu.addItem(userPartys.get(key), menuCommandDoNothing);

			}

		} else {
			topMenu.addItem("New Party", menuCommandNewParty);
		}

		topMenu.addSeparator();
		*/

		topMenu.addItem("Sign Out", true, menuCommandSignOut);

	}

	private void loadLogin() {

		// set up top menu
		topMenu.addItem("Sign In", true, menuCommandSignIn);
	}
}
